package com.senditcertified.mail;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.Utility.MessageManager;
import com.Utility.Utility;
import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.adapters.OutboxAdapter;
import com.adapters.OutboxAdapter1;
import com.controls.ws.OutboxWSControl;
import com.dialogs.LoadingDialog;
import com.events.WebServiceCommunicatorListener;
import com.objects.OutboxObject;

public class Outbox extends Activity implements WebServiceCommunicatorListener {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	private ExpandableListView myExpListView, myExpListView1;
	private TextView mTvMonth, mTvYear;
	private OutboxAdapter myAdapter;
	private OutboxAdapter1 myAdapter1;
	private String[] mMonths;
	private String[] mYears;
	private int mYear;
	private int currentYear;
	private int mMonth = 0;
	private Calendar calendar;
//	private HashMap<OutboxObject, List<OutboxObject>> myListData = new HashMap<OutboxObject, List<OutboxObject>>();
	
	private List<List<OutboxObject>> outboxDatas = new ArrayList<List<OutboxObject>>();
	private OutboxWSControl mWS;
	private ImageView mBtnView, mBtnRefresh;
	private LoadingDialog mDialog;
	private ArrayList<OutboxObject> myArrayOutbox;
	private View mFirstGroupList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.outbox);

		initViews();		
		initProperties();
	}

	private void initViews() {

		mTvMonth = (TextView) findViewById(R.id.tv_month);
		mTvYear = (TextView) findViewById(R.id.tv_year);

		myExpListView = (ExpandableListView) findViewById(R.id.expandablelist);
		myExpListView1 = (ExpandableListView) findViewById(R.id.expandablelist1);
		mMonths = getResources().getStringArray(R.array.months);
		calendar = Calendar.getInstance();
		mMonth = calendar.get(Calendar.MONTH);
		currentYear = calendar.get(Calendar.YEAR);
		SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
		mYears = new String[10];
		mYears[0] = yearFormat.format(calendar.getTime());
		for (int i = 1; i < 10; i++) {
			calendar.add(Calendar.YEAR, -1);
			mYears[i] = yearFormat.format(calendar.getTime());
		}

		mTvMonth.setText(mMonths[mMonth]);
		mTvYear.setText(mYears[0]);
		mBtnView = (ImageView) findViewById(R.id.btn_view);
		mBtnRefresh = (ImageView) findViewById(R.id.btn_refresh);
		mFirstGroupList = findViewById(R.id.firstGroupList);
	}

	private void initProperties() {
		mWS = new OutboxWSControl(this, this);
		mTvMonth.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialogPickMonth();
			}
		});
		mTvYear.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialogPickYear();
			}
		});

		mBtnView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				loadData();
			}
		});
		mBtnRefresh.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mMonth = calendar.get(Calendar.MONTH);
				mTvMonth.setText(mMonths[mMonth]);
				loadData();
			}
		});

		loadData();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		System.out.println("Stop");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		if (mWS != null) {
			mWS.cancel();
			mWS = null;
		}
		super.onDestroy();

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}

	private void loadData() {
		mWS.GetUserOutbox(
				Utility.getStringPreference(this, Utility.KEY_LOGIN_TOKEN, ""),
				String.valueOf(mMonth + 1), mTvYear.getText().toString());
	}

	public void loadData1() {
		outboxDatas.clear();
		
		if(myArrayOutbox.size() > 0){
			do{
				OutboxObject object = myArrayOutbox.get(0);
				String curUnique = object.getUniqueGUID();
				List<OutboxObject> items = new ArrayList<OutboxObject>();
				
				for (int i=0; i<myArrayOutbox.size(); i++) {
					OutboxObject tmp = myArrayOutbox.get(i);
					if(tmp.getUniqueGUID().equals(curUnique)){
						items.add(tmp);
						myArrayOutbox.remove(i);
						myArrayOutbox.trimToSize();
						i--;
					}
				}
				
				outboxDatas.add(items);
			}while(myArrayOutbox.size()>0);
		}
		myAdapter = new OutboxAdapter(this, outboxDatas);
		myExpListView.setAdapter(myAdapter);
		
		
		myAdapter1 = new OutboxAdapter1(this, outboxDatas);
		myExpListView1.setAdapter(myAdapter1);
		mFirstGroupList.setVisibility(View.VISIBLE);
	}


	private void showDialogPickMonth() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
		builder.setItems(mMonths, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mTvMonth.setText(mMonths[which]);
				mMonth = which;
			}
		});
		builder.create().show();
	}

	private void showDialogPickYear() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
		builder.setItems(mYears, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mTvYear.setText(mYears[which]);
				mYear = currentYear - which;
			}
		});
		builder.create().show();
	}

	@Override
	public void onConnectionOpen(WebServiceFlag flag) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		mDialog = new LoadingDialog(getParent());
		mDialog.setCancelable(false);
		mDialog.show();
	}

	@Override
	public void onConnectionError(WebServiceFlag flag, String error) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		MessageManager.showToast(this, error);
	}

	@Override
	public void onConnectionDone(WebServiceFlag flag, String response) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}

		if (flag == WebServiceFlag.GET_OUTBOX) {
			myArrayOutbox = mWS.parseOutbox(response);
			loadData1();
		}
	}
	
	public void onFirst3Dot(View v){
		mFirstGroupList.setVisibility(View.GONE);
	}

	public void onSecond3Dot(View v){
		mFirstGroupList.setVisibility(View.VISIBLE);
	}
}
